<template>
  <gj-loadmore :bottom-method="loadBottom" ref="loadmore">
    <div class="rockmans">
      <div class="row">
        <div class="col-xs-6 item" v-for="(item,index) in rockmans.list" :key="index">
          <a class="wrapper" href="javascript:;" @click.stop.prevent="toDetails(item)">
            <img :src="item.userHead" />
            <div>
              <div class="nickname">{{item.nickname}}</div>
              <div class="value" :class="item.class">{{item.totalFloatprofit}}</div>
              <div class="key">总收益率</div>
            </div>
          </a>
        </div>
      </div>
    </div>
  </gj-loadmore>
</template>

<script>
  import CommonUtil from 'common/common.util'
  import LocalDAO from 'common/localDAO'
  import RemoteDAO from 'common/remoteDAO'
  import LoadMore from 'components/LoadMore'
  export default {
    data () {
      return {
        userCode: LocalDAO.getUserCode(),
        pageSize: 20,
        pageNo: 1,
        rockmans: {
          list: []
        }
      }
    },
    components: {
      'gj-loadmore': LoadMore
    },
    methods: {
      //上拉加载
      loadBottom () {
        this.getData();
      },

      //获取点赞我的牛人列表
      getData () {
        RemoteDAO.callUC12008309(this.userCode, this.pageSize, this.pageNo, "2", "1", "1", "", "", "1", response => {
          if(response.error == 0){
            if(response.data.body.length > 0){
              let list = [];
              let targetUserCodes = [];
              response.data.body.forEach((item,index,array) => {
                let object = {};
                Object.assign(object,{
                  userCode: item.USER_CODE,
                  nickname: item.NICKNAME,
                  userHead: item.USER_HEAD == "" ? CommonUtil.getDefaultUserHead(item.USER_CODE) : item.USER_HEAD,
                  totalFloatprofit: "--%",
                  class: 'positive'
                });
                targetUserCodes.push(item.USER_CODE);
                list.push(object)
              })
              if(targetUserCodes.length > 0){
                RemoteDAO.callJCL30032(targetUserCodes.join(","),response => {
                  if(response.error == 0){
                    if(response.data.body.data.length > 0){
                      response.data.body.data.forEach((target,index,array) => {
                        let targetUserCode = target.client_id;
                        list.forEach((item,index,array) => {
                          if(item.userCode == targetUserCode){
                            let totalFloatprofit = CommonUtil.toFixed(target.total_floatprofit, 2);
                            if(totalFloatprofit >= 0){
                              if(totalFloatprofit.indexOf('+') != 0){
                                totalFloatprofit = '+' + totalFloatprofit;
                              }
                            }else{
                              if(totalFloatprofit.indexOf('-') != 0){
                                totalFloatprofit = '-' + totalFloatprofit;
                              }
                            }
                            item.totalFloatprofit = totalFloatprofit + '%';
                            item.class = item.totalFloatprofit.indexOf('-') >= 0 ? 'negative': 'positive';
                          }
                        })
                      })
                      this.rockmans.list = this.rockmans.list.concat(list);
                      // 更新分享页数据
                      var shareAdmirers = LocalDAO.getShareAdmirers();
                      if(shareAdmirers == null){
                        shareAdmirers = {};
                        shareAdmirers.rockmans = {};
                        shareAdmirers.rockmans.list = [];
                      }else if(typeof(shareAdmirers.rockmans.list) == "undefined"){
                        shareAdmirers.rockmans.list = [];
                      }
                      shareAdmirers.rockmans.list = this.rockmans.list;
                      LocalDAO.setShareAdmirers(shareAdmirers);
                      LocalDAO.setRockmansAdmirers(this.rockmans);
                      //上拉加载下拉刷新
                      if(list.length == this.pageSize){
                        this.pageNo = this.pageNo + 1;
                        this.$refs.loadmore.onBottomLoaded();//加载成功
                      }else{
                        this.$refs.loadmore.onBottomAllLoaded();//无更多数据
                      }
                    }else{
                      if(this.pageNo == 1){
                        this.$refs.loadmore.onBottomError("暂无数据");//暂无数据
                      }else{
                        this.$refs.loadmore.onBottomAllLoaded();//无更多数据
                      }
                    }
                  }else{
                    this.$refs.loadmore.onBottomError(response.data);//加载失败
                  }
                })
              }else{
                if(this.pageNo == 1){
                  this.$refs.loadmore.onBottomError("暂无数据");//暂无数据
                }else{
                  this.$refs.loadmore.onBottomAllLoaded();//无更多数据
                }
              }
            }else{
              if(this.pageNo == 1){
                this.$refs.loadmore.onBottomError("暂无数据");//暂无数据
              }else{
                this.$refs.loadmore.onBottomAllLoaded();//无更多数据
              }
            }
          }else{
            this.$refs.loadmore.onBottomError(response.data);//加载失败
          }
        })
      },

      //点击牛人头像跳转至牛人详情页面
      toDetails (item) {
        this.$router.push({path:'/otherdetail',query:{userCode:item.userCode}});
      }
    }
  }
</script>
